<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        body {
            background-color: #f1f1f1;
        }

        .swiper {
            width: 1226px;
            margin: 50px auto;
            position: relative;
        }

        .swiper-wrapper {}

        .swiper-wrapper .swiper-item {
            display: none;
            /* 透明度  0:完全透明  1:完全不透明 */
            opacity: 0;

            /* 让数值变化有一个缓慢过渡的效果 */
            transition: opacity 0.5s;
        }

        .swiper-wrapper .swiper-active {
            display: block;
            opacity: 1;
        }

        .swiper-wrapper img {
            width: 100%;
        }

        .nav {
            position: absolute;
            right: 20px;
            bottom: 10px;

            /* flex布局: 默认将元素从左向右排 */
            display: flex;

        }

        .nav li {
            width: 10px;
            height: 10px;
            background-color: rgba(0, 0, 0, 0.5);
            border: 1px solid rgba(0, 0, 0, 0.5);
            border-radius: 50%;
            margin: 0 7px;
        }

        .nav .active {
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div class="swiper">
        <div class="swiper-wrapper">
            <div class="swiper-item swiper-active"><img src="../images/1.png" alt=""></div>
            <div class="swiper-item"><img src="../images/2.webp" alt=""></div>
            <div class="swiper-item"><img src="../images/3.webp" alt=""></div>
            <div class="swiper-item"><img src="../images/4.webp" alt=""></div>
        </div>
        <div class="nav">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </div>
    </div>
</body>
<script>

    var swiper = document.querySelector(".swiper");
    var liList = document.querySelectorAll(".nav li");
    var swiperList = document.querySelectorAll(".swiper-wrapper .swiper-item");
    var index = 0; // 全局变量 => 记录轮播的下标
    var timer = null;

    autoPlay();  // 页面加载时启动自动轮播 

    // 点击切换
    for (let i = 0; i < liList.length; i++) {
        let li = liList[i];
        li.onclick = function () {
            index = i;  // 点击是记录轮播下标 => 下次从当前位置开启轮播
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
                swiperList[j].classList.remove("swiper-active");
            }
            liList[i].className = "active";
            swiperList[i].classList.add("swiper-active");
        }
    }

    swiper.onmouseover = function () {
        clearInterval(timer);
    }
    swiper.onmouseout = function () {
        autoPlay();
    }

  

    function autoPlay(){
        clearInterval(timer);
        timer = setInterval(function () {
            index++;

            // 超出下标的最大值 => 重置为0
            if (index >= liList.length) {
                index = 0;
            }

            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
                swiperList[j].classList.remove("swiper-active");
            }
            liList[index].className = "active";
            swiperList[index].classList.add("swiper-active");
        }, 3000)
    }



</script>

</html>